﻿using System.Linq.PageList;
using Dyao.Book.Application.Abstractions;
using Dyao.Book.Contract;
using Dyao.Book.Core;
using Dyao.Book.Web.Extensions;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace Dyao.Book.Web.Controllers
{
    public class TaskCenterController : ApiV1BaseController
    {
        private readonly ITaskCenterApplication _taskCenterService;
        private readonly ITaskCenterRecordApplication _taskCenterRecordService;

        /// <summary>
        /// TaskCenterController
        /// </summary>
        /// <param name="taskCenterService">taskCenterService</param>
        /// <param name="taskCenterRecordService">taskCenterRecordService</param>
        public TaskCenterController(ITaskCenterApplication taskCenterService,
            ITaskCenterRecordApplication taskCenterRecordService)
        {
            _taskCenterService = taskCenterService;
            _taskCenterRecordService = taskCenterRecordService;
        }

        /// <summary>
        /// 获取任务记录
        /// </summary>
        /// <param name="filter">filter</param>
        /// <param name="cancellationToken">cancellationToken</param>
        /// <returns></returns>
        [HttpPost("[action]")]
        public async Task<ApiResult<PageList<TaskCenterRecordPageDto>>> GetTaskCenterRecordModelPagedListAsync(TaskCenterRecordFilterDto filter, CancellationToken cancellationToken = default)
        {
            var orderBy = filter.GetOrderBy<TaskCenterRecordOrderByDto>();
            var pageFilter = filter.ToPageFilter(orderBy, filter);
            var pageList = await _taskCenterRecordService.GetTaskCenterRecordModelPagedListAsync(pageFilter, cancellationToken);
            return pageList;
        }

        /// <summary>
        /// 创建任务
        /// </summary>
        /// <param name="input">任务查询参数</param>
        /// <param name="cancellationToken">cancellationToken</param>
        /// <returns></returns>
        [HttpPost("[action]")]
        public async Task<ApiResult<int>> CreateTaskAsync([FromBody][ModelBinder(typeof(TaskCenterCreateInputModelBinder))] TaskCenterCreateInputDto input, CancellationToken cancellationToken = default)
        {
            this.ValidateModelState();

            var taskModel = new TaskCenterCreateDto
            {
                TaskCode = input.TaskCode,
                MenuPath = input.MenuPath,
            };

            ITaskCenterParameters parameters = new DefaultTaskCenterParameters(input.ParametersString, input.ExtraParameters, (type, value) => JsonConvert.DeserializeObject(value?.ToString(), type));

            var taskId = await _taskCenterService.CreateTaskAsync(taskModel, parameters, cancellationToken);

            return taskId;
        }


        /// <summary>
        /// 删除任务记录
        /// </summary>
        /// <param name="taskCenterRecordDeleteModel">taskCenterRecordDeleteModel</param>
        /// <param name="cancellationToken">cancellationToken</param>
        /// <returns></returns>
        [HttpPost("[action]")]
        public async Task<ApiResult> DeleteRecordAsync(TaskCenterRecordDeleteDto taskCenterRecordDeleteModel, CancellationToken cancellationToken = default)
        {
            await _taskCenterRecordService.DeleteAsync(taskCenterRecordDeleteModel, cancellationToken);
            return Ok();
        }

        ///// <summary>
        ///// 根据动作key获取实体导出列
        ///// </summary>
        ///// <param name="taskCode">动作编码</param>
        ///// <returns></returns>
        //[HttpGet("[action]")]
        //public ApiResult<IReadOnlyList<string>> GetExportColumns(string taskCode)
        //{
        //    var exportService = GetRequiredService<IExportTaskService>();
        //    return exportService.GetExportCenterColumns(taskCode);
        //}

        ///// <summary>
        ///// 根据动作key获取实体分组导出列
        ///// </summary>
        ///// <param name="taskCode">动作编码</param>
        ///// <returns></returns>
        //[HttpGet("[action]")]
        //public ApiResult<Dictionary<string, IEnumerable<string>>> GetExportGroupingColumns(string taskCode)
        //{
        //    var exportService = GetRequiredService<IExportTaskService>();
        //    return exportService.GetExportCenterGroupingColumns(taskCode);
        //}
    }
}
